package com.example.springbootquartz.config;

import com.example.springbootquartz.task.Task;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * 配置任务
 */
@Configuration
public class QuartzConfiguration {

    /**
     * 调用的工作类
     * @param task 调用的工作类
     * @return
     */
    @Bean(name = "taskJob1")
    public MethodInvokingJobDetailFactoryBean testJobBean(Task task) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(false);
        // 需要执行的对象
        jobDetail.setTargetObject(task);
        /*
         * TODO  非常重要
         * 执行类中的需要执行方法
         */
        jobDetail.setTargetMethod("myTask1");
        return jobDetail;
    }

    /**
     * 调用的工作类
     * @param task 调用的工作类
     * @return
     */
    @Bean(name = "taskJob2")
    public MethodInvokingJobDetailFactoryBean testJobBean1(Task task) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(false);
        // 需要执行的对象
        jobDetail.setTargetObject(task);
        /*
         * TODO  非常重要
         * 执行类中的需要执行方法
         */
        jobDetail.setTargetMethod("myTask2");
        return jobDetail;
    }

    /**
     * 定时触发器
     * @Qualifier bean的名称
     */
    @Bean(name = "taskJobTrigger1")
    public CronTriggerFactoryBean testJobTrigger1(@Qualifier("taskJob1") JobDetail jobDetail1) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(jobDetail1);
        //每1秒执行一次
        tigger.setCronExpression("*/1 * * * * ?");
        // tigger.setName("reptilianTrigger");
        return tigger;
    }

    /**
     * 定时触发器
     * @Qualifierbean的名称
     */
    @Bean(name = "taskJobTrigger2")
    public CronTriggerFactoryBean testJobTrigger2(@Qualifier("taskJob2") JobDetail jobDetail2) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(jobDetail2);
        //每2秒执行一次
        tigger.setCronExpression("*/2 * * * * ?");
        // tigger.setName("reptilianTrigger");
        return tigger;
    }

    /**
     * 调度工厂
     *
     * @return
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(@Qualifier("taskJobTrigger1") Trigger trigger1
            , @Qualifier("taskJobTrigger2") Trigger trigger2) {
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        factoryBean.setOverwriteExistingJobs(true);
        // 延时启动，应用启动1秒后
        factoryBean.setStartupDelay(1);
        // 注册触发器
        factoryBean.setTriggers(trigger1, trigger2);

        return factoryBean;
    }

}